package com.puhui.lib.utils.encrypt;

import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * 加密工具类
 *
 * @author tangjian
 * @date 2015年6月5日
 */
@SuppressWarnings("unused")
public class AesEncryptUtil {
    private static final String ALGORITHM = "AES";

//    public static void main(String[] args) {
//        String source = "tangjian";
//        String password = "a123456";
////        String resultThree = encrypt(source, password);
////        if (null != resultThree) {
////            String resultFour = decrypt(resultThree, password);
////        }
//
//        byte[] bytes = encrypt(source, password);
//        if (null != bytes) {
//            decrypt(bytes, password);
//        }
//    }

    /**
     * Dn DongNao
     *
     * @param content  要加密的内容
     * @param password 加密随机数
     */
    public static byte[] encrypt(String content, String password) {
        try {
            KeyGenerator instance = KeyGenerator.getInstance(ALGORITHM);
            //用password作为随机数初始化
            instance.init(128, new SecureRandom(password.getBytes()));
            //得到一个秘钥
            SecretKey secretKey = instance.generateKey();
            //对秘钥进行基本的编码
            byte[] encoded = secretKey.getEncoded();
            //转换成AES专用的秘钥
            SecretKeySpec spec = new SecretKeySpec(encoded, ALGORITHM);
            //创建一个密码器
            Cipher cipher = Cipher.getInstance(ALGORITHM);

            //开始加密
            cipher.init(Cipher.ENCRYPT_MODE, spec);
            String result = new String(cipher.doFinal(content.getBytes()));
            System.out.println("DMLog*******加密后内容：".concat(result));
            return cipher.doFinal(content.getBytes());
        } catch (NoSuchAlgorithmException | NoSuchPaddingException
                | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     *
     * @param content  解密的内容
     * @param password 加密随机数
     */
    public static String decrypt(byte[] content, String password) {
        try {
            //创建AES的key生产者
            KeyGenerator instance = KeyGenerator.getInstance(ALGORITHM);
            //利用password作为随机数初始化
            instance.init(128, new SecureRandom(password.getBytes()));
            //根据password生产一个秘钥(所以对此算法通用)
            SecretKey secretKey = instance.generateKey();
            //对秘钥进行基本的编码
            byte[] encoded = secretKey.getEncoded();
            //转换成AES专用的秘钥 RoundKey
            SecretKeySpec keySpec = new SecretKeySpec(encoded, ALGORITHM);
            //创建一个解密器
            Cipher cipher = Cipher.getInstance(ALGORITHM);

            //开始解密
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            String result = new String(cipher.doFinal(content));
            System.out.println("DMLog*******解密后内容：".concat(result));
            return result;
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
                | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密
     *
     * @param content  解密的内容
     * @param password 加密随机数
     */
    public static String decrypt(String content, String password) {
        try {
            //创建AES的key生产者
            KeyGenerator instance = KeyGenerator.getInstance(ALGORITHM);
            //利用password作为随机数初始化
            instance.init(128, new SecureRandom(password.getBytes()));
            //根据password生产一个秘钥(所以对此算法通用)
            SecretKey secretKey = instance.generateKey();
            //对秘钥进行基本的编码
            byte[] encoded = secretKey.getEncoded();
            //转换成AES专用的秘钥 RoundKey
            SecretKeySpec keySpec = new SecretKeySpec(encoded, ALGORITHM);
            //创建一个解密器
            Cipher cipher = Cipher.getInstance(ALGORITHM);

            //开始解密
            cipher.init(Cipher.DECRYPT_MODE, keySpec);
            String result = new String(cipher.doFinal(content.getBytes()));
            System.out.println("DMLog*******解密后内容：".concat(result));
            return result;
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException
                | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Base64加密
     */
    public static String base64Encrypt(String data, String key) {
        if (data == null) {
            return "";
        }
        String encode = "";
        try {
            encode = new String(Base64.encode(data.getBytes("utf-8"), Base64.NO_WRAP));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        encode += key;
        byte[] b = encode.getBytes();
        for (int i = 0; i < b.length / 2; i++) {
            byte temp = b[i];
            b[i] = b[b.length - 1 - i];
            b[b.length - 1 - i] = temp;
        }
        return new String(b);
    }
}